package com.lw.extraceim.zxing;

import androidx.annotation.NonNull;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;

import com.journeyapps.barcodescanner.camera.CameraManager;
import com.lw.extraceim.R;
import com.lw.extraceim.app.AppConstants;
import com.lw.extraceim.ui.activity.es.WriteExpressSheetActivity;
import com.lw.extraceim.ui.activity.base.BaseActivity;

/**
 * @Author : liwei
 * @Email : 1759840027@qq.com
 * @Description : 自定义的条形码扫描界面
 * @Date : 2019/4/13 9:06
 */
public class ExtraceCaptureActivity extends BaseActivity implements View.OnClickListener {
    public final static int SCAN_NEW_ES = 10001;
    public final static int SCAN_SEARCH = 10002;
    public final static int SCAN_NEW_PACKAGE = 10003;
    public final static int SCAN_UNPACK_PACKAGE = 10004;
    public final static int SCAN_TRANSPORT_PACKAGE = 10005;
    public final static int SCAN_DELIVER_ES = 10006;
    public final static int SCAN_UNPACK_ES = 1007;
    public final static int SCAN_ADD_DELIVER_ES = 1008;

    private CaptureManager capture;
    private DecoratedBarcodeView barcodeScannerView;
    private CameraManager cameraManager;

    private ImageButton mFlash;
    private ImageButton mBack;
    private Button mEsNo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        barcodeScannerView = initializeContent();
        cameraManager = new CameraManager(getApplication());
        capture = new CaptureManager(this, barcodeScannerView);
        capture.initializeFromIntent(getIntent(), savedInstanceState);
        capture.decode();
    }

    /**
     * Override to use a different layout.
     *
     * @return the DecoratedBarcodeView
     */
    protected DecoratedBarcodeView initializeContent() {
        return (DecoratedBarcodeView) findViewById(R.id.zxing_barcode_scanner);
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_extrace_capture);
        mFlash = findViewById(R.id.btn_flash);
        mFlash.setOnClickListener(this);
        mBack = findViewById(R.id.btn_back);
        mBack.setOnClickListener(this);
        mEsNo = findViewById(R.id.btn_es_no);
        mEsNo.setOnClickListener(this);
        if(application.getScanActivityRequestCode() != SCAN_NEW_ES){
            mEsNo.setVisibility(View.INVISIBLE);//将快递单号设置为不可见
        }
    }

    @Override
    public void initData() {

    }

    @Override
    public void refreshUI() {

    }

    @Override
    public void initEvent() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        capture.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        capture.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        capture.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        capture.onSaveInstanceState(outState);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
        capture.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return barcodeScannerView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_flash: {
                onOrOffFlash();
                break;
            }
            case R.id.btn_back: {
                finish();
                break;
            }
            case R.id.btn_es_no: {
                WriteExpressSheetActivity.actionStart(this, AppConstants.ACTION_NEW, null);
                break;
            }
        }
    }

    //闪光灯是否开启
    private boolean isFlashOn = false;

    @Override
    public void onBackPressed() {
        finish();
    }

    /**
     * 开关闪光灯
     */
    public void onOrOffFlash() {
        Toast.makeText(this, "打开或关闭闪光灯", Toast.LENGTH_SHORT).show();
        if (!isFlashOn) {
            cameraManager.setTorch(true);
            mFlash.setImageResource(R.drawable.flash_on);
            isFlashOn = true;
        } else {
            cameraManager.setTorch(false);
            mFlash.setImageResource(R.drawable.flash_off);
            isFlashOn = false;
        }
    }

}
